
const _interopRequireDefault = require('@babel/runtime/helpers/interopRequireDefault');

exports.__esModule = true;
exports.default = void 0;

const _utils = require('../../utils');

const _email = require('../../utils/validate/email');

const _number = require('../../utils/validate/number');

const _cell = _interopRequireDefault(require('../../cell'));

const _field = _interopRequireDefault(require('../../field'));

const _cellGroup = _interopRequireDefault(require('../../cell-group'));

const _SkuImgUploader = _interopRequireDefault(require('./SkuImgUploader'));

// Utils
// Components
const _createNamespace = (0, _utils.createNamespace)('sku-messages');
const createComponent = _createNamespace[0];
const bem = _createNamespace[1];
const t = _createNamespace[2];

const _default2 = createComponent({
  props: {
    messages: {
      type: Array,
      default: function _default() {
        return [];
      },
    },
    messageConfig: Object,
    goodsId: [Number, String],
  },
  data: function data() {
    return {
      messageValues: this.resetMessageValues(this.messages),
    };
  },
  watch: {
    messages: function messages(val) {
      this.messageValues = this.resetMessageValues(val);
    },
  },
  methods: {
    resetMessageValues: function resetMessageValues(messages) {
      const { messageConfig } = this;
      const _messageConfig$initia = messageConfig.initialMessages;
      const initialMessages = _messageConfig$initia === void 0 ? {} : _messageConfig$initia;
      return (messages || []).map((message) => ({
        value: initialMessages[message.name] || '',
      }));
    },
    getType: function getType(message) {
      if (+message.multiple === 1) {
        return 'textarea';
      }

      if (message.type === 'id_no') {
        return 'text';
      }

      return message.datetime > 0 ? 'datetime-local' : message.type;
    },
    getMessages: function getMessages() {
      const _this = this;

      const messages = {};
      this.messageValues.forEach((item, index) => {
        let { value } = item;

        if (_this.messages[index].datetime > 0) {
          value = value.replace(/T/g, ' ');
        }

        messages[`message_${index}`] = value;
      });
      return messages;
    },
    getCartMessages: function getCartMessages() {
      const _this2 = this;

      const messages = {};
      this.messageValues.forEach((item, index) => {
        let { value } = item;
        const message = _this2.messages[index];

        if (message.datetime > 0) {
          value = value.replace(/T/g, ' ');
        }

        messages[message.name] = value;
      });
      return messages;
    },
    getPlaceholder: function getPlaceholder(message) {
      const type = +message.multiple === 1 ? 'textarea' : message.type;
      const map = this.messageConfig.placeholderMap || {};
      return message.placeholder || map[type] || t(`placeholder.${type}`);
    },
    validateMessages: function validateMessages() {
      const values = this.messageValues;

      for (let i = 0; i < values.length; i++) {
        const { value } = values[i];
        const message = this.messages[i];

        if (value === '') {
          // 必填字段的校验
          if (String(message.required) === '1') {
            const textType = t(message.type === 'image' ? 'upload' : 'fill');
            return textType + message.name;
          }
        } else {
          if (message.type === 'tel' && !(0, _number.isNumeric)(value)) {
            return t('invalid.tel');
          }

          if (message.type === 'mobile' && !/^\d{6,20}$/.test(value)) {
            return t('invalid.mobile');
          }

          if (message.type === 'email' && !(0, _email.isEmail)(value)) {
            return t('invalid.email');
          }

          if (message.type === 'id_no' && (value.length < 15 || value.length > 18)) {
            return t('invalid.id_no');
          }
        }
      }
    },
    genMessage: function genMessage(message, index) {
      const _this3 = this;

      const h = this.$createElement;

      if (message.type === 'image') {
        return h(_cell.default, {
          key: `${this.goodsId}-${index}`,
          attrs: {
            title: message.name,
            label: t('imageLabel'),
            required: String(message.required) === '1',
            valueClass: bem('image-cell-value'),
          },
          class: bem('image-cell'),
        }, [h(_SkuImgUploader.default, {
          attrs: {
            maxSize: this.messageConfig.uploadMaxSize,
            uploadImg: this.messageConfig.uploadImg,
          },
          model: {
            value: _this3.messageValues[index].value,
            callback: function callback($$v) {
              _this3.$set(_this3.messageValues[index], 'value', $$v);
            },
          },
        })]);
      }

      return h(_field.default, {
        attrs: {
          maxlength: '200',
          label: message.name,
          required: String(message.required) === '1',
          placeholder: this.getPlaceholder(message),
          type: this.getType(message),
        },
        key: `${this.goodsId}-${index}`,
        model: {
          value: _this3.messageValues[index].value,
          callback: function callback($$v) {
            _this3.$set(_this3.messageValues[index], 'value', $$v);
          },
        },
      });
    },
  },
  render: function render() {
    const h = arguments[0];
    return h(_cellGroup.default, {
      class: bem(),
      attrs: {
        border: this.messages.length > 0,
      },
    }, [this.messages.map(this.genMessage)]);
  },
});

exports.default = _default2;
